var appPath = getAppPath();

$(function(){
	// 检索日期范围
	$("#queryDateStart").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	$("#queryDateEnd").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	// 初始化列表
	initstaHisGrid();
});

function dtFmt(dt){
	var y = dt.getFullYear();
    var m = dt.getMonth()+1;
    var d = dt.getDate();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
}
function dtParser(s){
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        return new Date(y,m-1,d);
    } else {
        return new Date();
    }
}

function initstaHisGrid(){
	$('#staHisGrid').datagrid({
	    url:appPath + "/itbstahis/pageOrderStatehisInfo.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    nowrap: false,// 换行
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'his_id',title:'记录ID',width:30,align:'center',sortable:false,
  	        	formatter:function(value,rowData,rowIndex){
  	        		return '<span style="color:red;">'+value+'</span>';
  	        	}
  	        },
  	        {field:'type',title:'分类',width:30,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
  	        		var t = '<span style="color:red">未知</span>';
  	        		switch(Number(value||'0')){
  	        			case 10: t='<span style="color:#00CD00;">商品订单</span>'; break;
  	        			case 20: t='<span style="color:#FF8C69;">发票</span>'; break;
  	        			case 30: t='<span style="color:#00C5CD;">购买模板</span>'; break;
  	        			case 40: t='<span style="color:#1874CD;">虚拟商品</span>'; break;
  	        			case 50: t='<span style="color:#FF7F00;">秒杀订单</span>'; break;
  	        		}
  	        		return t;
	        	}
		    },
  	        {field:'trade_obj_id',title:'交易对象ID',width:30,align:'center',sortable:false},
	        {field:'state',title:'状态',width:30,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
  	        		//订单状态:10待支付、11已支付、12支付失败、20制作中、30配送中、40完成、50取消
  	        		var s = '<span style="color:#838B8B;">未知</span>';
  	        		switch(Number(value||'0')){
  	        			case 10: s='<span style="color:#00E5EE;">待支付</span>'; break;
  	        			case 11: s='<span style="color:green">已支付</span>'; break;
  	        			case 12: s='<span style="color:red;">失败</span>'; break;
  	        			case 20: s='<span style="color:#00E5EE;">制作中</span>'; break;
  	        			case 30: s='<span style="color:#00E5EE;">配送中</span>'; break;
  	        			case 40: s='<span style="color:#00E5EE;">完成</span>'; break;
  	        			case 50: s='<span style="color:#00E5EE;">取消</span>'; break;
  	        		}
  	        		return s;
	        	}
		    },
		    {field:'total_fee',title:'订单总金额',width:35,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var s = '<span style="color:#838B8B;">0/元</span>';
		    		if(value){
		    			s = '<span style="color:#4F4F4F;">'+value/100+'/元</span>';
		    		}
  	        		return s;
		    	}
		    },
		    {field:'desc',title:'记录描述',width:65,align:'left',sortable:false},
	        {field:'create_time',title:'创建时间',width:40,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:40,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[
			/*{
				text:'删除记录',
				iconCls:'icon-cancel',
				handler:function(){
					delOrder();
				}
			}*/
		]
	});
}



function getQueryParam(){
	var queryObject = {};
	var findState = $("#queryState").combobox('getValue');
	var queryObjId = $("#queryObjId").val();
	var findHisId = $("#queryHisId").val();
	var findDtS = $("#queryDateStart").datebox('getValue');
	var findDtE = $("#queryDateEnd").datebox('getValue');
	if(queryObjId && Number(queryObjId||'0')>0) queryObject.findObjId = queryObjId;//交易对象ID
	if(findHisId && Number(findHisId||'0')>0) queryObject.findHisId = findHisId;
	if(findState && Number(findState||'0')>0) queryObject.findType = findState;//分类
	if(findDtS && findDtS.length>0) queryObject.findDateStart = findDtS+' 00:00:00';
	if(findDtE && findDtE.length>0) queryObject.findDateEnd = findDtE+' 23:59:59';
	return queryObject;
}


/**
 * 查询数据
 */
function doQuery(){
	var findDtS = $("#queryDateStart").datebox('getValue');
	var findDtE = $("#queryDateEnd").datebox('getValue');
	if(findDtS && findDtS.length>0){
		var chkRet = chkInputDate(findDtS);
		$("#queryDateStart").datebox('setValue',chkRet);
	}
	if(findDtE && findDtE.length>0){
		var chkRet = chkInputDate(findDtE);
		$("#queryDateEnd").datebox('setValue',chkRet);
	}
	
	$("#staHisGrid").datagrid('unselectAll');
	$("#staHisGrid").datagrid('load', getQueryParam());
}

// 判断日期格式[YYYY-MM-DD]
function chkInputDate(dt){
	var strDate=dt+'';
    var ss = (strDate.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        var chkDt = new Date(y,m-1,d);
		if((chkDt.getFullYear()==y) &&((chkDt.getMonth()+1)==m) &&(chkDt.getDate()==d)) {
        	return dtFmt(chkDt);
        }
    }
    return '';
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
